// 任务管理应用
document.addEventListener('DOMContentLoaded', function() {
    // 获取DOM元素
    const taskForm = document.getElementById('task-form');
    const taskInput = document.getElementById('task-input');
    const taskList = document.getElementById('task-list');
    const totalTasksElement = document.getElementById('total-tasks');
    const completedTasksElement = document.getElementById('completed-tasks');
    
    // 从localStorage加载任务或初始化空数组
    let tasks = JSON.parse(localStorage.getItem('tasks')) || [];
    
    // 初始化应用
    function initApp() {
        renderTasks();
        updateTaskCount();
    }
    
    // 渲染任务列表
    function renderTasks() {
        taskList.innerHTML = '';
        
        if (tasks.length === 0) {
            taskList.innerHTML = '<p class="empty-message">暂无任务，添加一个新任务吧！</p>';
            return;
        }
        
        tasks.forEach((task, index) => {
            const taskElement = document.createElement('div');
            taskElement.className = `task-item ${task.completed ? 'completed' : ''}`;
            taskElement.innerHTML = `
                <input type="checkbox" class="task-checkbox" ${task.completed ? 'checked' : ''} data-index="${index}">
                <span class="task-text">${task.text}</span>
                <div class="task-actions">
                    <button class="delete-btn" data-index="${index}">删除</button>
                </div>
            `;
            taskList.appendChild(taskElement);
        });
        
        // 添加事件监听器
        document.querySelectorAll('.task-checkbox').forEach(checkbox => {
            checkbox.addEventListener('change', toggleTaskCompleted);
        });
        
        document.querySelectorAll('.delete-btn').forEach(button => {
            button.addEventListener('click', deleteTask);
        });
    }
    
    // 添加新任务
    function addTask(e) {
        e.preventDefault();
        
        const taskText = taskInput.value.trim();
        if (taskText === '') return;
        
        tasks.push({
            text: taskText,
            completed: false,
            createdAt: new Date().toISOString()
        });
        
        saveTasks();
        renderTasks();
        updateTaskCount();
        
        // 清空输入框
        taskInput.value = '';
        taskInput.focus();
    }
    
    // 切换任务完成状态
    function toggleTaskCompleted(e) {
        const index = e.target.dataset.index;
        tasks[index].completed = !tasks[index].completed;
        saveTasks();
        renderTasks();
        updateTaskCount();
    }
    
    // 删除任务
    function deleteTask(e) {
        const index = e.target.dataset.index;
        tasks.splice(index, 1);
        saveTasks();
        renderTasks();
        updateTaskCount();
    }
    
    // 保存任务到localStorage
    function saveTasks() {
        localStorage.setItem('tasks', JSON.stringify(tasks));
    }
    
    // 更新任务计数
    function updateTaskCount() {
        const totalTasks = tasks.length;
        const completedTasks = tasks.filter(task => task.completed).length;
        
        totalTasksElement.textContent = totalTasks;
        completedTasksElement.textContent = completedTasks;
    }
    
    // 事件监听器
    taskForm.addEventListener('submit', addTask);
    
    // 初始化应用
    initApp();
});